<?php
/**
 * Translateone Controller for yos_translator Component
 * @package		yos_translator
 * @subpackage	Components
 * @link		http://yopensource.com
 * @author		yopensource
 * @copyright 	yopensource (yopensource@gmail.com)
 * @license		Commercial
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'phpWebHacks.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'find_replace.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'resource_url.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'autocuttext.php');

JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
jimport('joomla.filesystem.file');
require_once(JPATH_SITE.DS.'libraries'.DS.'joomla'.DS.'filesystem'.DS.'archive'.DS.'zip.php');

/**
 * Translator Extension Controller
 *
 * @package		yos_translator
 * @subpackage	Components
 */
class FVBControllerFeed extends FVBController
{

	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();
		$this->registerTask('new', 'edit');
		$this->registerTask('apply', 'save');
	}
	
	function edit(){
		$document 	= &JFactory::getDocument();
		$vType		= $document->getType();

		$vLayout 	= 'edit';
		$mName		= 'feeds';
		// Get/Create the view
		$view = &$this->getView( 'feeds', $vType);
		// Get/Create the model

		if ($model = &$this->getModel($mName)) {
			// Push the model into the view (as default)

			$view->setModel($model, true);
		}
		// Set the layout
		$view->setLayout($vLayout);
		$view->edit();
	}
	
	/**
	 * Step1: get document type
	 *
	 */
	function get_document_type(){
		$model = &$this->getModel('feeds');
		$type=$model->get_document_type();
		echo $type;
		die();
	}
	
	function step2_test(){
		$model = &$this->getModel('feeds');
		$links=$model->step2_test();
		echo $links;
		die();
	}
	
	function step2_test_link(){
		$model = &$this->getModel('feeds');
		$links=$model->step2_test_link();
		echo $links;
		die();
	}
	
	function step3_test(){		
		$model = &$this->getModel('feeds');
		$titles=$model->step3_test();
		echo $titles;
		die();
	}
	function step4_test(){		
		$model = &$this->getModel('feeds');
		$dates=$model->step4_test();
		echo $dates;
		die();
	}
	function step4_test_a_link(){		
		$model = &$this->getModel('feeds');
		$dates=$model->step4_test_a_link();
		echo $dates;
		die();
	}
	function step5_test(){
		$model = &$this->getModel('feeds');
		$intros=$model->step5_test();
		echo $intros;
		die();
	}
	
	function step5_test_a_link(){
		$model = &$this->getModel('feeds');
		$obj_intro=$model->step5_test_a_link();
		echo $obj_intro;
		die();
	}
	
	function step6_test(){
		$model = &$this->getModel('feeds');
		$fulls=$model->step6_test();
		echo $fulls;
		die();
	}
	
	function step7_test(){
		$model = &$this->getModel('feeds');
		$images=$model->step7_test();
		echo $images;
		die();
	}
	
	function step8_test(){
		$model = &$this->getModel('feeds');
		$content_multipage=$model->step8_test();
		echo $content_multipage;
		die();
	}	
	function save(){
		global $mainframe;
		
		$model 			=	 &$this->getModel('feeds');
		$row			=	$model->save();
		$post			= 	JRequest::get( 'post' );
		switch ( JRequest::getCmd('task') )
		{
			case 'feed.apply':
				$msg 	=	JText::_( 'Changes to feed saved' );
				$mainframe->redirect( "index.php?option=".$post['option']."&task=feed.edit&cid[]=". $row->id, $msg );
				break;

			case 'feed.save':
			default:
				$msg 	=	JText::_( 'Feed saved' );
				$mainframe->redirect( "index.php?option=".$post['option']."&task=feeds", $msg );
				break;
		}
	}
	function publish(){
		global $mainframe, $option;
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
		$row =& JTable::getInstance('feeds','Table');
		$cids 	= JRequest::getVar('cid', array(0), 'post', 'array');
		$err=0;
		$user=JFactory::getUser();
		$task=JRequest::getVar('task');
		foreach ($cids as $cid)
		{
			$row->load($cid);
			if ( JTable::isCheckedOut($user->get ('id'), $row->checked_out )) {
				$err++;
				continue;
			}			
			$row->published= ($task=='feed.publish')?1:0;		
			$row->store();
		}
		$sucses=count($cids)-$err;
		$mess="";
		$task=explode('.',$task);
		if($sucses)
			$mess.=$sucses.' '.JText::_('feed(s) successfully '.$task[1]).". &nbsp; ";
		if ($err)
			$mess.=$err.' '.JText::_('feed(s) is being edited by another administrator');
		
		$mainframe->redirect( "index.php?option=$option&task=feeds" ,$mess);
	}
	function cancel(){
		global $mainframe,$option;
		// Set the table directory
		$mainframe->redirect( "index.php?option=$option&task=feeds" );
	}

	function remove(){
		global $mainframe, $option;
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$model = &$this->getModel('feeds');
		$model->remove();
		$mess="";
		if ($model->errRemove==null){
			$mess=JText::_('Link(s): Delete is successfully');			
		}
		else {
			$arrErr=$model->errRemove;
			if (count($arrErr)==1) {
					$mess=JText::_("There is");
			}
			else {
					$mess=JText::_("There are");
			}
			$mess.=' '.JText::_('links could not be delete').': ';
			JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
			$row =& JTable::getInstance('FVB_Crawler','Table');	
			$ds="";
			foreach ($arrErr as $key=>$jobs_title)
			{
				$row->load($key);
				$mess.=$ds."$key($jobs_title)";
				$ds=", ";
			}
			$mess.=JText::_('. Need to delete any job is using these links');
		}
		$mainframe->redirect( "index.php?option=$option&task=feeds", $mess);
	}
/*	*/
function saveorder()
	{
		global $mainframe;
		// Initialize variables
		$db			= & JFactory::getDBO();

		$cid		= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$order		= JRequest::getVar( 'order', array (0), 'post', 'array' );
		$total		= count($cid);
		$conditions	= array ();

		JArrayHelper::toInteger($cid, array(0));
		JArrayHelper::toInteger($order, array(0));

		// Instantiate an article table object
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
		$row =& JTable::getInstance('feeds','Table');

		// Update the ordering for items in the cid array
		for ($i = 0; $i < $total; $i ++)
		{
			$row->load( (int) $cid[$i] );
			if ($row->ordering != $order[$i]) {
				$row->ordering = $order[$i];
				if (!$row->store()) {
					JError::raiseError( 500, $db->getErrorMsg() );
					return false;
				}
				
			}
		}

		$msg = JText::_('Feed ordering saved');
		$mainframe->redirect('index.php?option=com_feed_manager', $msg);
		
	}
	function orderup()
	{
		$this->orderContent(-1);
	}
	function orderdown()
	{
		$this->orderContent(1);
	}
	
	function orderContent($direction)
	{
		global $mainframe;
		// Initialize variables
		$db		= & JFactory::getDBO();

		$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
		
		if (isset( $cid[0] ))
		{	
			$row =& JTable::getInstance('feeds','Table');
			$row->load( (int) $cid[0] );
			$row->move($direction );
		}

		$mainframe->redirect('index.php?option=com_feed_manager&task=feeds');
	}
}

